package cn.aitrox.ry.service.anno.controller;

import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.controller.BaseController;
import cn.aitrox.ry.common.service.api.dto.User;
import cn.aitrox.ry.service.anno.api.UserTaskApi;
import cn.aitrox.ry.service.anno.dto.user.task.in.TaskAnnoInfoInDto;
import cn.aitrox.ry.service.anno.dto.user.task.in.TaskJoinableSearchInDto;
import cn.aitrox.ry.service.anno.dto.user.task.in.UserTaskJoinInDto;
import cn.aitrox.ry.service.anno.dto.user.task.in.UserTaskSearchInDto;
import cn.aitrox.ry.service.anno.dto.user.task.out.UserTaskItemDto;
import cn.aitrox.ry.service.anno.dto.user.task.series.in.UserTaskSeriesSearchInDto;
import cn.aitrox.ry.service.anno.dto.user.task.series.out.UserTaskSeriesDto;
import cn.aitrox.ry.service.anno.service.UserTaskService;
import cn.aitrox.ry.service.user.dto.in.UserListInDto;
import cn.aitrox.ry.service.user.feign.UserFeign;
import cn.aitrox.ry.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class UserTaskController extends BaseController implements UserTaskApi {

    @Autowired
    private UserTaskService userTaskService;

    @Autowired
    @Lazy
    private UserFeign userFeign;

    @GetMapping(value = "/anno/user/doing/task/list")
    @Override
    public List<UserTaskItemDto> userDoingTaskList() {
        return userTaskService.userDoingTaskList(super.getUserId());
    }

    @PostMapping(value = "/anno/user/task/search")
    @Override
    public CommonPageInfo<UserTaskItemDto> search(@RequestBody UserTaskSearchInDto input) {
        return userTaskService.search(super.getUserId(), input);
    }

    @PostMapping(value = "/anno/task/joinable/search")
    @Override
    public CommonPageInfo<UserTaskItemDto> joinableSearch(@RequestBody TaskJoinableSearchInDto input) {
        return userTaskService.joinableSearch(super.getUserId(), input);
    }

    @PostMapping(value = "/anno/task/join")
    @Override
    public RespResult userTaskJoin(@Validated @RequestBody UserTaskJoinInDto input) {
        return userTaskService.userTaskJoin(super.getUserId(), input.getTaskIds());
    }

    @PostMapping(value = "/anno/user/task/series/search")
    @Override
    public CommonPageInfo<UserTaskSeriesDto> userTaskSeriesSearch(@RequestBody UserTaskSeriesSearchInDto input) {
        CommonPageInfo<UserTaskSeriesDto> pageInfo = userTaskService.userTaskSeriesSearch(input);
        if (!ArrayUtil.isEmpty(pageInfo.getList())) {
            List<Integer> userIds = pageInfo.getList().stream().map(UserTaskSeriesDto::getUserId).distinct().collect(Collectors.toList());
            List<User> userList = userFeign.list(new UserListInDto(userIds));
            Map<Integer, String> userIdNameMap = userList.stream().collect(Collectors.toMap(User::getId, User::getUserName));
            pageInfo.getList().forEach(x -> {
                x.setUserName(userIdNameMap.get(x.getUserId()));
            });
        }
        return pageInfo;
    }

    @PostMapping(value = "/anno/task/anno/info")
    @Override
    public RespResult<UserTaskItemDto> taskAnnoInfo(@RequestBody TaskAnnoInfoInDto input) {
        return userTaskService.taskAnnoInfo(input.getTaskId(), input.getUserId());
    }
}
